  
-- Cheat Menu -  Cheat menu for Grand Theft Auto SanAndreas
-- Copyright (C) 2019-2020 Grinch_

-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
local module = {}

module.tanimation =
{
    fighting      =
    {
        selected  = imgui.new.int(fconfig.Get('tanimation.fighting.selected',1)),
        names     = {"Default","Boxing","Kung fu","Kick Boxing","Punch Kick"},
    }, 
    filter = imgui.ImGuiTextFilter(),
    ifp_name      = imgui.new.char[20](),
    list          = fcommon.LoadJson("animation"),
    loop          = imgui.new.bool(fconfig.Get('tanimation.loop',false)),
    name          = imgui.new.char[20](),
    ped           = imgui.new.bool(fconfig.Get('tanimation.ped',false)),
    secondary     = imgui.new.bool(fconfig.Get('tanimation.secondary',false)),
    walking       = 
    { 
        selected  = imgui.new.int(fconfig.Get('tanimation.walking.selected',1)),
        names     = {"default","man","shuffle","oldman","gang1","gang2","oldfatman","fatman","jogger","drunkman","blindman","swat","woman","shopping","busywoman","sexywoman","pro","oldwoman","fatwoman","jogwoman","oldfatwoman","skate"},
    },
}

function PlayAnimation(file,animation)
    if module.tanimation.ped[0] == true then
        if doesCharExist(fped.tped.selected) then
            char = fped.tped.selected
        else
            printHelpString("~r~No~w~ ped selected")
            return
        end
    else
        char = PLAYER_PED
    end

    if file ~= "PED" then   -- don't remove if animation is from ped.ifp
        requestAnimation(file)  
        loadAllModelsNow()
    end

    if module.tanimation.secondary[0] == true then
        taskPlayAnimSecondary(char,animation,file,4.0,module.tanimation.loop[0],0,0,0,-1)  
    else
        taskPlayAnim(char,animation,file,4.0,module.tanimation.loop[0],0,0,0,-1)
    end
    fcommon.CheatActivated()
    if file ~= "PED" then   -- don't remove if animation is from ped.ifp
        removeAnimation(file)  
    end
end

-- Main function
function module.AnimationMain()
    imgui.Spacing()
    if imgui.Button("Stop animation",imgui.ImVec2(fcommon.GetSize(1))) then
        local char = nil
        if module.tanimation.ped[0] == true then
            if fped.tped.selected ~=  nil then
                char = fped.tped.selected
            else
                printHelpString("~r~No~w~ ped selected")
                return
            end
        else
            char = PLAYER_PED
        end
        clearCharTasks(char)
        fcommon.CheatActivated()
    end
    imgui.Spacing()

    -- Checkboxes
    imgui.Columns(3,nil,false)
    fcommon.CheckBoxVar("Loop##Animation",module.tanimation.loop)
    imgui.NextColumn()
    fcommon.CheckBoxVar("Ped##Animation",module.tanimation.ped,"Play animation on ped.Aim with a gun to select.")
    imgui.NextColumn()
    fcommon.CheckBoxVar("Secondary##Animation",module.tanimation.secondary)
    imgui.Columns(1)

    if fcommon.BeginTabBar('Animation') then
        if fcommon.BeginTabItem('Search') then
            fcommon.DrawEntries(fconst.IDENTIFIER.ANIMATION,fconst.DRAW_TYPE.TEXT,function(anim,file)
				PlayAnimation(file,anim)
			end,
            function(text,category)
                if imgui.MenuItemBool("Remove animation") then 
                    module.tanimation.list[category][text] = nil
                    
                    local bool = false
                    for k,v in pairs(module.tanimation.list[category]) do
                        bool = true
                    end
                    
                    if not bool then
                        module.tanimation.list[category] = nil
                    end

                    printHelpString("Animation ~r~removed")
                end
            end,function(a) return a end,module.tanimation.list)
        end
        if fcommon.BeginTabItem('Misc') then
            if fcommon.DropDownListNumber("Fighting",module.tanimation.fighting.names,module.tanimation.fighting.selected) then
                giveMeleeAttackToChar(PLAYER_PED,module.tanimation.fighting.selected[0]+3,6)
                fcommon.CheatActivated()
            end
            if fcommon.DropDownListNumber("Walking",module.tanimation.walking.names,module.tanimation.walking.selected) then
                if module.tanimation.walking.names[module.tanimation.walking.selected[0]] == "default" then
                    writeMemory(0x609A4E,4,0x4D48689,false)
                    writeMemory(0x609A52,2,0,false)
                else
                    writeMemory(0x609A4E,4,-0x6F6F6F70,false)
                    writeMemory(0x609A52,2,0x9090,false)
                    
                    requestAnimation(module.tanimation.walking.names[module.tanimation.walking.selected[0]])
                    loadAllModelsNow()
                    setAnimGroupForChar(PLAYER_PED,module.tanimation.walking.names[module.tanimation.walking.selected[0]])
                    removeAnimation(module.tanimation.walking.names[module.tanimation.walking.selected[0]])
                end
                fcommon.CheatActivated()
            end
        end
        if fcommon.BeginTabItem('Custom') then
            imgui.InputTextWithHint("File","ped",module.tanimation.ifp_name,ffi.sizeof(module.tanimation.ifp_name))
            imgui.InputTextWithHint("Animation##input","cower",module.tanimation.name,ffi.sizeof(module.tanimation.name))
            imgui.Spacing()
            if imgui.Button("Add animation",imgui.ImVec2(fcommon.GetSize(1))) then
                if ffi.string(module.tanimation.ifp_name) == "" then
                    printHelpString("No file name found")
                else
                    if ffi.string(module.tanimation.name) == "" then
                        printHelpString("No animation name found")
                    else
                        if module.tanimation.list[ffi.string(module.tanimation.ifp_name)] == nil then
                            module.tanimation.list[ffi.string(module.tanimation.ifp_name)] = {}
                        end
                        module.tanimation.list[ffi.string(module.tanimation.ifp_name)][ffi.string(module.tanimation.name)] = ffi.string(module.tanimation.name)
        
                        printHelpString("Animation ~g~added")
                    end
                end
               
            end
        end
        fcommon.EndTabBar()
    end
end

return module